<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\GoldCharge;

class GoldChargesController extends AdminController
{
    protected $title = '金店账户充值';
    
    protected function grid()
    {
        $grid = new Grid(new GoldCharge());
	    $grid->model()->orderByDesc('id');
	
	    if ($userId = request('user_id')) {
		    $grid->model()->where('user_id', $userId)->orderByDesc('id');
	    }

        $grid->column('id', 'ID');
	    $grid->column('user', '会员')->display(function ($value) {
		    return $value ? $this->user->nameText(true) : '';
	    });
        $grid->column('number', '单号');
        $grid->column('total_amount', '金额');
	    $grid->column('paid_at', '付款时间')->display(function () {
		    return $this->paid_at ? (string)$this->paid_at : '待付款...';
	    });
	    $grid->column('paid_method', '付款方式')->display(function ($value) {
		    if (empty($value)) {return "<i class='fa fa-times-circle text-default'></i> 未支付";}
		    $method = config("site.payment.methods.{$value}");
		    $method = empty($method) ? $value : $method;
		    $text = "<i class='fa fa-check-circle text-success'></i> {$method}";
		    return $text;
	    });
	    $grid->column('created_at', '时间')->display(function () {
		    return (string)$this->created_at;
	    });
	
	    $this->gridFilter($grid);
	
	    $grid->disableRowSelector();
	    $grid->disableCreateButton();
	    $grid->disableExport();
	    $grid->disableActions();

        return $grid;
    }
	
	protected function gridFilter(Grid $grid)
	{
		$grid->filter(function (Grid\Filter $filter) {
			$filter->expand();
			$filter->column(1/2, function (Grid\Filter $filter) {
				$filter->disableIdFilter();
				$filter->equal('user_id', '会员 ID 查询');
			});
		});
	}
}
